﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;

namespace ProductSystem.BLL.Common
{
    public class HTTPHelper
    {
        /// <summary>
        /// 获取网站内容
        /// </summary>
        /// <param name="Url">地址</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string GetHtml(string Url, string encoding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            string htmlStr = "";
            try
            {
                request = (HttpWebRequest)WebRequest.Create(Url);
                request.Timeout = 20000;
                request.AllowAutoRedirect = false;

                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
                {
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), Encoding.GetEncoding(encoding));
                    }
                    else
                    {
                        reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                    }
                    htmlStr = reader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                LogHelper.Instance().WriteLog("获取网站html内容时程序出错", e);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (reader != null)
                {
                    reader.Close();
                }
                if (request != null)
                {
                    request = null;
                }
            }
            return htmlStr;
        }

        /// <summary>
        /// 以流的形式下载图片
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="filePath">图片文件夹</param>
        /// <param name="fileName">图片文件名</param>
        public static bool GetImgFromStream(string url, string filePath, string fileName)
        {
            bool flag = false;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                response = (HttpWebResponse)request.GetResponse();
                if (response.ContentType.ToLower().StartsWith("text/"))
                {
                    return flag;
                }
                Image image = Image.FromStream(response.GetResponseStream());
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string file = filePath + @"\" + fileName;
                image.Save(file);
                image.Dispose();

            }
            catch (Exception e)
            {
                LogHelper.Instance().WriteLog("以流的形式保存图片时程序出错！", e);
                return flag;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (request != null)
                {
                    request = null;
                }
                flag = true;

            }
            return flag;
        }
    }
}
